package main

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"log"
)

type User struct {
	Name   string `json:"name"`
	Age    int    `json:"age"`
	Gender string `json:"gender"`
}

func main() {
	router := gin.Default()

	router.Use(cors.New(cors.Config{
		AllowMethods:     []string{"PUT", "PATCH", "POST", "GET", "DELETE"},
		AllowHeaders:     []string{"Origin"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			return true
		},
	}))
	router.GET("/", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"msg":  "welcome hashdata",
			"code": 0,
		})
	})
	router.GET("/user", func(c *gin.Context) {
		var u = []User{
			{"John", 25, "Male"},
			{"Jane", 30, "Female"},
			{"Jim", 35, "Male"},
			{"Judy", 40, "Female"},
		}
		c.JSON(200, gin.H{
			"data": u,
			"msg":  "success",
			"code": 0,
		})
	})
	if err := router.Run(":9000"); err != nil {
		log.Println(err)
	}
}
